package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.settings.commons.constant.Constants;
import com.bjpowernode.crm.settings.commons.domain.ReturnObject;
import com.bjpowernode.crm.settings.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    //数据处理层一定有业务逻辑层的对象
    @Autowired//(required = false)表示有相关bean,就使用没有就跳过
    private UserService userService;
    /**
     * url要和controller方法处理完请求之后，响应信息返回的页面的资源目录保持一致
     */
    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        //请求转发到登录页面
        System.out.println("正在请求转发");
        return "settings/qx/user/login";
    }

    @ResponseBody
    @RequestMapping("/settings/qx/user/login.do")
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("正在登录");
        //封装参数
        Map<String,Object> map=new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        //调用service层的方法，查询用户
        User user=userService.queryUserByLoginActAndPwd(map);

        //根据查询结果，生成响应信息
        ReturnObject returnObject=new ReturnObject();
        if (user==null){
            //登陆失败,用户名或者密码错误
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
        }else{//进一步判断账号是否合法
            //将当前系统时间转换成字符串进行比较,使用CompareTo比较两个字符串,equals比较两个地址是否指向同一个地址
            String nowStr=DateUtils.formatDateTime(new Date());
            if (nowStr.compareTo(user.getExpireTime())>0){
                //表示已经过期,已经失效
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号过期");
            }else if ("0".equals(user.getLockState())){
                //登陆失败，ip受限
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("ip受限");
            }else {
                //登陆成功
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
                //把user保存到session中
                session.setAttribute(Constants.SESSION_USER,user);

                //如果需要记住密码，则往外写cookie
                if ("true".equals(isRemPwd)){
                    Cookie c1=new Cookie("loginAct", user.getLoginAct());
                    Cookie c2=new Cookie("loginPwd", user.getLoginPwd());
                    c1.setMaxAge(60*60);
                    c2.setMaxAge(60*60);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }else{
                    //把没有过期的cookie删除
                    Cookie c1=new Cookie("loginAct", "1");
                    Cookie c2=new Cookie("loginPwd", "1");
                    c1.setMaxAge(0);
                    c2.setMaxAge(0);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }
            }
        }
        return returnObject;
    }

    @RequestMapping("settings/qx/user/logout.do")
    public String logout(HttpServletResponse response,HttpSession session){
        //清空cookie
        Cookie c1=new Cookie("loginAct", "1");
        Cookie c2=new Cookie("loginPwd", "1");
        c1.setMaxAge(0);
        c2.setMaxAge(0);
        response.addCookie(c1);
        response.addCookie(c2);
        //销毁session
        session.invalidate();
        //跳转首页
        return "redirect:/";//借助springmvc重定向
    }
}
